<template>
	<xn-form-container
		:title="formData.id ? '编辑客户信息' : '增加客户信息'"
		:width="1400"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="horizontal">
			<a-form-item label="名称：" name="name">
				{{ formData.name }}
			</a-form-item>
			<a-form-item label="说明：" name="description">
				{{ formData.description }}
			</a-form-item>
			<a-table
				class="ant-table-striped"
				ref="tableRef"
				:columns="columns"
				:data-source="tableDatas"
				bordered
				:row-key="(record) => record.id"
				:pagination="{ page: page, pageSize: pageSize, total: total, current: current }"
				@change="loadData"
				:row-class-name="(_record, index) => (_record.endDate ? (check(_record.endDate) ? 'expired' : 'good') : 'good')"
			>
				<template #bodyCell="{ column, record }">
					<template v-if="column.dataIndex === 'buildState'">
						{{ $TOOL.dictTypeData('BUILD_STATE', record.buildState) }}
					</template>
					<template v-if="column.dataIndex === 'approvalStatus'">
						{{ $TOOL.dictTypeData('APPROVAL_STATUS', record.approvalStatus) }}
					</template>
					<template v-if="column.dataIndex === 'state'">
						{{ $TOOL.dictTypeData('LICENSE_STATUS', record.state) }}
					</template>
					<template v-if="column.dataIndex === 'type'">
						{{ $TOOL.dictTypeData('LICENSE_TYPE', record.type) }}
					</template>
					<template v-if="column.dataIndex === 'action'">
						<a-space>
							<a @click="formAppendRef.onOpen(record)" v-if="record.endDate&&check(record.endDate)">续期</a>
							<a @click="formViewRef.onOpen(record)">查看</a>
						</a-space>
					</template>
				</template>
			</a-table>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
		</template>
		<Extend ref="formAppendRef" @successful="loadData" />
		<FormView ref="formViewRef"  />
	</xn-form-container>
</template>

<script setup name="customerInfoForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import customerInfoApi from '@/api/biz/customerInfoApi'
	import licenseApplicationApi from '@/api/biz/licenseApplicationApi'
	import Extend from '@/views/biz/licenseapplication/extend.vue'
	import FormView from '@/views/biz/licenseapplication/view.vue'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const tableRef = ref()
	const formRef = ref()
	const formAppendRef = ref()
	const formViewRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const architectureOptions = ref([])
	const tableDatas = ref([])
	const current = ref(0)
	const pageSize = ref(20)
	const page = ref()
	const total = ref()
	const columns = [
		{
			title: '版本',
			dataIndex: 'versionName'
		},
		{
			title: '开始时间',
			dataIndex: 'startDate'
		},
		{
			title: '结束时间',
			dataIndex: 'endDate'
		},
		{
			title: '生成时间',
			dataIndex: 'createDate'
		},
		{
			title: '最大用户',
			dataIndex: 'maxUser'
		},
		{
			title: 'MAC地址',
			dataIndex: 'macAddress'
		},
		{
			title: '发放状态',
			dataIndex: 'state'
		},
		{
			title: '授权类型',
			dataIndex: 'type'
		},
		{
			title: '操作',
			dataIndex: 'action'
		}
	]

	const loadData = (pagination) => {
		//debugger
		if (formData.value) {
			//debugger
			const searchFormParam = {}
			//debugger
			if (pagination) {
				searchFormParam.current = pagination.current
				searchFormParam.size = pagination.pageSize
			} else {
				searchFormParam.current = current.value
				searchFormParam.size = pageSize.value
			}
			searchFormParam.customerId = formData.value.id
			return licenseApplicationApi.licenseApplicationPage(Object.assign({}, searchFormParam)).then((res) => {
				tableDatas.value = res.records
				page.value = res.pages
				pageSize.value = res.size
				current.value = res.current
				total.value = res.total
			})
		}
	}
	const change = () => {}
	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		architectureOptions.value = tool.dictList('ARCH')
		loadData()
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		name: [required('请输入名称')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				customerInfoApi
					.customerInfoSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	const check = (endDate) => {
		function getDateInTimezone(date, timezone) {
			return new Date(date.toLocaleString('en-US', { timeZone: timezone }))
		}
		// 计算明天的日期
		let tomorrow = getDateInTimezone(new Date(), 'UTC')
		//tomorrow.setDate(today.getDate() + 1)
		tomorrow.setHours(0, 0, 0, 0) // 确保时间部分为0，只比较日期部分

		// 要比较的日期
		let someDate = new Date(endDate) // 举例，你可以替换为任何你想要比较的日期
		someDate.setHours(0, 0, 0, 0) // 确保时间部分为0，只比较日期部分
		// console.log('someDate',someDate)
		// console.log('tomorrow',tomorrow)
		// console.log('-------------------')
		// 比较日期
		if (someDate >= tomorrow) {
			return false
		} else {
			return true
		}
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
<style lang="less">
	.ant-table-striped .expired td {
		color: rgba(255, 0, 0, 1);
	}
	.ant-table-striped .good td {
		color: rgba(51, 177, 52, 1);
	}
</style>
